const api = require('../../utils/api.js')
//获取应用实例
const app = getApp()

Page({
  data: {
    user: {},
    birthday: {}
  },
  onLoad: function() {
    this.getUser();
  },
  save: function(e) {
    api.postForm('/app/saveInfo.json', e.detail.value).then(res => {
      this.getUser();
    }).catch(() => {});
  },
  bindDateChange: function(e) {
    this.setData({
      birthday: e.detail.value
    })
  },
  chooseImage: function(e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        api.uploadFile('/app/uploadAvatar.json', res.tempFilePaths[0], 'file', (r) => {
          if (r) {
            that.getUser();
          }
        });
      }
    })
  },
  getUser: function() {
    api.getData('/app/userInfo.json').then(res => {
      this.setData({
        user: res.data
      });
      this.setData({
        birthday: res.data.birthday
      })
    }).catch(() => {});
  },
  logout: function() {
    api.logout();
  }
})